<?php
namespace app\admin\controller;
use app\common\controller\Common;
use app\admin\model\CategoryModel;
use app\common\model\Category as CategoryDb;
use app\common\model\Attribute;
use think\Exception;
use think\Request;

/**
* 分类管理
*/
class Category extends Common
{
	/**
	* 添加商品分类信息
	*/
	public function addcategory()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(CategoryModel::setCategory($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
			
		}
	
		$this->assign('category',CategoryModel::FCate());
		return $this->fetch();	
	}
	/**
	* 商品分类列表
	*/
	public function categorylist()
	{
		$this->assign('catlist',CategoryModel::getCategory());
		return $this->fetch();
	}
	/**
	* 修改分类
	*/
	public function editcategory()
	{
		$info = Request::instance()->param();
		if(request()->isPost())
		{
			try{
				return json(CategoryModel::editCategory($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
			
		}
		
		$this->assign('catinfo',CategoryModel::getCategoryInfo($info['id']));
		$this->assign('category',CategoryModel::FCate());
		return $this->fetch();
	}
	/**
	* 删除分类信息
	*/
	public function delcategory()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(CategoryModel::delCategory($info['id']));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
	}
	/**
	* 获取下级分类
	*/
	public function lowerCate()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(CategoryModel::lowerCate($info['id']));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
	}
    /**
    * 展示分类信息
    */
    public function categoryinfo()
    {
        $CategoryDb = new CategoryDb();
        $Attribute = new Attribute();
        /*产品分类*/
        $fuji = $CategoryDb->categoryList(['fid'=>0],'category_id,category_name');
        $this->assign('fuji',$fuji);
        $zijidiyige = $CategoryDb->categoryList(['fid'=>$fuji[0]['category_id']],'category_id,category_name');
        $this->assign('zijidiyige',$zijidiyige);
        /*产品品牌*/
        $pingpai = $Attribute->attributeList(['type'=>0],'attribute_name');
        $this->assign('pingpai',$pingpai);
        /*产品内存*/
        $neicun = $Attribute->attributeList(['type'=>1],'attribute_name');
        $this->assign('neicun',$neicun);
        /*产品属性*/
        $shuxing = $Attribute->attributeList(['type'=>2],'attribute_name');
        $this->assign('shuxing',$shuxing);
        /*产品颜色*/
        $yanse = $Attribute->attributeList(['type'=>3],'attribute_name');
        $this->assign('yanse',$yanse);
        return $this->fetch();
    }
    /**
    * 获取下级分类
    */
    public function catego()
    {
        if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(CategoryModel::categoFcate($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
			
		}
    }
	/***************************************产品属性**************************************/
	/**
	* 添加属性
	*/
	public function addattribute()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(CategoryModel::setAttribute($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
			
		}
		
		$this->assign('category',CategoryModel::towCategory());
		return $this->fetch();
	}
	/**
	* 属性列表
	*/
	public function attributelist()
	{
		if(request()->isPost())
		{
			$info = Request::instance()->param();
			return (new CategoryModel())->getAttributeList(10,$info['page']);
		}
		return $this->fetch();
	}
	/**
	* 修改属性
	*/
	public function editattribute()
	{
		$info = Request::instance()->param();
		if(request()->isPost())
		{
			try{
				return json(CategoryModel::editAttribute($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
		
		$this->assign('attribute',CategoryModel::getAttributeInfo($info['id']));
		$this->assign('category',CategoryModel::towCategory());
		return $this->fetch();
	}
	/**
	* 删除属性信息
	*/
	public function delattribute()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(CategoryModel::delAttribute($info['id']));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
	}
	/**
	* 获取下级分类
	*/
	public function lowerBute()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(CategoryModel::lowerIbute($info['id']));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
	}
}
